package com.culturalCenter.dataCenter.mapper.site;

import com.culturalCenter.dataCenter.customEntity.activity.CustomSiteActivity;
import com.culturalCenter.dataCenter.customEntity.responseParam.site.SiteInfoResponse;
import com.culturalCenter.dataCenter.customEntity.site.CustomSite;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteBooking;
import com.culturalCenter.dataCenter.customEntity.user.CustomListUser;
import com.culturalCenter.dataCenter.entity.site.Site;
import com.culturalCenter.dataCenter.mapper.base.BaseMapper;
import com.culturalCenter.dataCenter.vo.SiteUsedDataVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * Site的Dao
 */
@Mapper
public interface SiteMapper extends BaseMapper<Site> {

    /**
     * 统计能使用的场地总数
     *
     * @return
     */
    @Select("SELECT IFNULL(COUNT(1),0) FROM cdc_site WHERE status=1")
    int selectTotal();

    /**
     * 根据片区查场地列表
     *
     * @param area
     * @param date
     *
     * @return
     */
    List<CustomSiteBooking> findSiteStatusByArea(@Param("area") String area, @Param("date") Date date);

    /**
     * 根据时间片区查场地列表
     *
     * @param area
     * @param beginTime
     * @param endTime
     *
     * @return
     */
    List<CustomSiteBooking> searchSiteStatusByArea(@Param("area") String area, @Param("beginTime") Date beginTime,@Param("endTime") Date endTime);


    List<CustomSiteBooking> searchSiteStatusByAreaAfter(@Param("area") String area, @Param("beginTime") Date beginTime,@Param("endTime") Date endTime);

    /**
     * 获取场地片区
     *
     * @return
     */
    @Select("SELECT area FROM cdc_site WHERE status=1 GROUP BY area")
    List<String> getSiteArea();

    /**
     * 获取场地类型列表
     *
     * @return
     */
    @Select("SELECT DISTINCT category FROM cdc_site WHERE status=1")
    List<String> getSiteTypeList();

    /**
     * 获取场地列表
     *
     * @param siteName
     * @param siteType
     *
     * @return
     */
    List<CustomSite> selectCustomSite(@Param("siteName") String siteName, @Param("siteType") String siteType);

    /**
     * 获取场地基本信息
     *
     * @param siteCode
     *
     * @return
     */
    SiteInfoResponse selectSiteInfo(String siteCode);

    /**
     * 根据筛选条件查询数量
     *
     * @param siteName
     * @param siteType
     *
     * @return
     */
    int countByNameAndType(@Param("siteName") String siteName, @Param("siteType") String siteType);

    /**
     * 查询场地（根据场地编码）
     *
     * @param siteCode
     *
     * @return
     */
    @Select("SELECT siteName, code,category, location, galleryful, contacts, phone, openTime FROM cdc_site  WHERE code = #{siteCode} LIMIT 1")
    CustomSiteActivity selectBySiteCode(String siteCode);

    /**
     * 批量插入场地
     *
     * @param siteList
     */
    void insertBatch(@Param("list") List<Site> siteList);

    /**
     * 获取场地开放类型列表
     *
     * @return
     */
    @Select("SELECT CASE openType WHEN '1' THEN '1' WHEN '2' THEN '2' WHEN '3' THEN '3' END FROM cdc_site GROUP BY openType")
    List<String> getSiteOpenTypeList();

    /**
     * 获取场地开放类型统计
     *
     * @param openType
     *
     * @return
     */
    @Select("SELECT COUNT(1) FROM cdc_site WHERE openType = #{openType} AND status=1")
    String countByOpenType(String openType);

    /**
     * 获取场地类型统计
     *
     * @param category
     *
     * @return
     */
    @Select("SELECT COUNT(1) FROM cdc_site WHERE category = #{category} AND status=1")
    String countBySiteType(String category);

    /**
     * 统计场地类型“其他”
     *
     * @return
     */
    @Select("SELECT COUNT(1) FROM cdc_site WHERE (category = '其他' OR category IS NULL) AND status=1")
    int countBySiteTypeIsOther();

    /**
     * 获取场地refId（根据siteCode）
     *
     * @param siteCode
     *
     * @return
     */
    @Select("SELECT DISTINCT siteId AS id FROM cdc_site_booking  WHERE siteCode=#{siteCode} LIMIT 1")
    String getSiteIdBySiteCode(String siteCode);

    /**
     * 该场地可容纳人数（根据场地code获得）
     *
     * @param siteCode
     *
     * @return
     */
    @Select("SELECT galleryful FROM cdc_site WHERE code=#{siteCode}")
    Integer getGalleryfulBySiteCode(String siteCode);

    /**
     * 查全部场地使用状态
     *
     * @param siteId
     *
     * @return
     */
    @Select("SELECT code FROM cdc_site WHERE refId=#{siteId}")
    String getSiteCodeBySiteId(String siteId);

    /**
     * 获取该场地类型参与率最高的20个用户
     *
     * @param siteType
     *
     * @return
     */
    List<CustomListUser> getUsersBySiteType(String siteType);

    @Select("SELECT id  FROM placeManage.tb_site WHERE code = #{siteCode}")
    String selectSiteIdBySiteCode(String siteCode);

    SiteUsedDataVo getSiteUsedData(@Param("siteCode") String siteCode,
                                   @Param("startDate") Date startDate,
                                   @Param("endDate") Date endDate);
}
